clear all
set more off, perm
global cd 	"/mnt/brownresearch/ECON_LaPorta/rlaporta/research/BGLS2023"
global WorkXS  	$cd/XS/DataWork
global Temp    	$cd/PNL/DataWork
global WRDS  	$cd/WRDS
global Tables   $cd/PNL/DataForTables
/* ********************************************************************* */
* Get CRSP data generated by XS/CodeGenDataTables/DoFiles/read_crsp.do 
use if ret<. using $WorkXS/msf_adjusted, clear

* Deal with obs with missing mcap data (mostly IPOs)
egen id=group(permco permno) 
tsset id datem

generate Lmcap=L.mcap 	if L.mcap>0
replace  Lmcap=mcap 	if Lmcap==. & mcap>0
keep if ret+Lmcap<.
/* ********************************************************************* */
* Deal with firms that have multiple classes of shares (adjustment plays no role for 98.55% with only one share class).
* Compute vw return for firms with multiple securities; sort data so that i select the permno for the security with the largest market cap @t
duplicates tag permco datem, gen(dup)
tabulate dup

sort permco datem mcap
collapse (lastnm) comnam permno siccd exchcd date (sum) Mcap=mcap (mean) ret [aweight=Lmcap] , by(permco datem)

tsset permco datem
forvalues x=1(1)60 {
	quietly: generate   ret`x'=F`x'.ret 
}
rename Mcap F0Mcap
order *, sequential

keep  datem date comnam permco permno siccd exchcd  F0Mcap ret1-ret60 
order datem date comnam permco permno siccd exchcd  F0Mcap ret1-ret60 
* *******************************
save $Temp/ret, replace
* *******************************
* Compute dividend yield
import sas $WRDS/CRSP/msf.sas7bdat, clear case(lower)
keep permno permco date prc shrout ret retx

gen datem=mofd(date)
format datem %tm

egen id=group(permco permno)
tsset id datem
replace prc=abs(prc)

gen Div=(L1.prc)*(ret-retx)
gen CumDiv=Div
forvalues x=1(1)11 {
	replace CumDiv=CumDiv+L`x'.Div
}
generate non_div=1 if CumDiv==0
replace  non_div=0 if CumDiv<. & CumDiv >0
replace  non_div=. if CumDiv==.

generate pd=ln(prc)-ln(CumDiv)
replace  pd=0	if non_div==1
replace  pd=. 	if CumDiv==.
gen dp=ln(1+CumDiv/prc)

keep permco permno datem non_div pd
tempfile dividend
save "`dividend'", replace
* *************************************************************************************** Get IBES data generated by XS/CodeGenDataTables/DoFiles/read_ibes.do 
use  ticker permco permno comnam cname datem statpers LTG mcap if LTG<. using $WorkXS/XSibes.dta, clear
replace LTG=LTG/100

* merge with returns; In case of multiple listings, keep IBES data for security with largest market cap
merge 1:1 permco permno datem using $Temp/ret
keep if _merge==3
drop _merge
* *****************************************************
merge 1:1 permco permno datem using "`dividend'"
keep if _merge==3
drop _merge
* *****************************************************
* set rho to value generated by "TimeSeriesForXS.do" 
scalar rho=.97787029 

forvalues x=1(1)5 {
	quietly: gen RetYr`x'=1
}
forvalues x=1(1)12 {
	quietly: replace RetYr1=RetYr1*(1+ret`x')
}
forvalues x=13(1)24 {
	quietly: replace RetYr2=RetYr2*(1+ret`x')
}
forvalues x=25(1)36 {
	quietly: replace RetYr3=RetYr3*(1+ret`x')
}
forvalues x=37(1)48 {
	quietly: replace RetYr4=RetYr4*(1+ret`x')
}
forvalues x=49(1)60 {
	quietly: replace RetYr5=RetYr5*(1+ret`x')
}
su permco RetYr*

gen r1=ln(RetYr1)
gen r3=ln(RetYr1)+rho^1*ln(RetYr2)+rho^2*ln(RetYr3)
gen r5=ln(RetYr1)+rho^1*ln(RetYr2)+rho^2*ln(RetYr3)+rho^3*ln(RetYr4)+rho^4*ln(RetYr5)

save $Work/new, replace
* ***********
* Get compustat data generated by XS/CodeGenDataTables/DoFiles/Compustat.do 
merge 1:1 permco permno datem using $WorkXS/qtly.dta, keepusing(permco permno datem ibps)

* assume anlaysts know earnings @ t-3 when they forecast LTG
tsset permno datem
gen error =ln(F57.ibps)/5-ln(L3.ibps)/5-LTG
gen error3=ln(F33.ibps)/3-ln(L3.ibps)/5-LTG
gen ge5=ln(L3.ibps)/5-ln(L63.ibps)/5
gen pe = ln(F0Mcap) - ln(L3.ibps)

keep if _merge==3
keep permco permno datem LTG error error3 pd pe ge5 r3 r5 
* **************************************************************************************
keep if LTG<. 
keep if dofm(datem)<=mdy(12,31,2020)
* *********
save $Tables/DataTablesReplication_PNL, replace
* **************************************************************************************
!rm $Temp/ret.dta -f
!rm $Temp/new.dta -f
